package org.huang.spock.spring

import org.huang.spock.spring.bo.Dept
import org.huang.spock.spring.bo.Person
import org.huang.spock.spring.param.QueryDeptPageParam
import org.huang.spock.spring.service.DemoService
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import spock.lang.Specification

import java.util.stream.Collectors


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
//@ActiveProfiles("test")
class SpringBeanMockTest extends Specification {

    @Autowired
    private DemoService demoService;

    //spring 集成接口测试
    def "test getByDeptId"() {
        when:
        def result = demoService.getByDeptId(id)
        then:
        with(result) {
            dept == expectDept
            persons.stream().map(Person::getName).collect(Collectors.toList()) == expectPersonNames
        }
        where:
        id || expectDept                                 | expectPersonNames
        1  || new Dept(id: 1, name: '技术部', code: '0010') | ['张三', '里斯', '王五']
        2  || new Dept(id: 2, name: '运营部', code: '0020') | ['哈哈', '呵呵', '嘿嘿']
    }

    def "test listDeptPage"() {
        when:
        def result = demoService.listDeptPage(new QueryDeptPageParam(page: page, pageSize: 2))
        then:
        result.stream().map(Dept::getName).collect(Collectors.toList()) == expectPersonNames
        where:
        page || expectPersonNames
        1    || ['技术部', '运营部']
        2    || []
    }

}